package com.wlw.admin.openeye.ui.adapter

import android.content.Context
import android.graphics.Typeface
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.hazz.kotlinmvp.view.recyclerview.MultipleType
import com.hazz.kotlinmvp.view.recyclerview.ViewHolder
import com.hazz.kotlinmvp.view.recyclerview.adapter.CommonAdapter
import com.wlw.admin.openeye.R
import com.wlw.admin.openeye.durationFormat
import com.wlw.admin.openeye.glide.GlideRoundTransform
import com.wlw.admin.openeye.mvp.model.bean.HomeBean
import com.wlw.admin.openeye.showToast

class VideoDetailAdapter(mContext: Context, data: ArrayList<HomeBean.Issue.Item>) : CommonAdapter<HomeBean.Issue.Item>(mContext, data, object : MultipleType<HomeBean.Issue.Item> {
    override fun getLayoutId(item: HomeBean.Issue.Item, position: Int): Int {
        return when {

            position == 0 -> R.layout.item_video_detail

            data[position].type == "textCard" -> R.layout.item_video_text_card

            data[position].type == "videoSmallCard" -> R.layout.item_video_small_card

            else -> throw IllegalAccessException("Api 解析出错了，出现其他类型")

        }
    }
}) {
    private var textTypeface: Typeface? = null

    init {
        textTypeface = Typeface.createFromAsset(mContext.assets, "fonts/FZLanTingHeiS-L-GB-Regular.TTF")
    }

    fun addData(item: HomeBean.Issue.Item) {
        mData.clear()
        mData.add(item)
        notifyDataSetChanged()
    }

    fun addData(item: ArrayList<HomeBean.Issue.Item>) {
        mData.addAll(item)
        notifyDataSetChanged()
    }

    private var mOnItemClickRelatedVideo: ((item: HomeBean.Issue.Item) -> Unit)? = null
    fun setOnItemDetailClick(mItemRelatedVideo: (item: HomeBean.Issue.Item) -> Unit) {
        this.mOnItemClickRelatedVideo = mItemRelatedVideo
    }

    override fun bindData(holder: ViewHolder, data: HomeBean.Issue.Item, position: Int) {
        when {
            position == 0 -> setVideoDetailInfo(data, holder)
            data.type == "textCard" -> setTextCard(data, holder)
            data.type == "videoSmallCard" -> setVideoSmallCard(data, holder)
            else -> throw IllegalAccessException("Api 解析出错了，出现其他类型")
        }

    }

    private fun setVideoSmallCard(data: HomeBean.Issue.Item, holder: ViewHolder) {
        with(holder) {
            setText(R.id.tv_title, data.data?.title!!)
            setText(R.id.tv_tag, "#${data.data.category}/${durationFormat(data.data.duration)}")
            setImagePath(R.id.iv_video_small_card, object : ViewHolder.HolderImageLoader(data.data.cover.detail) {
                override fun loadImage(iv: ImageView, path: String) {

                    Glide.with(mContext).load(path)
                            .into(iv)

                }
            })
            holder.itemView.setOnClickListener {
                mOnItemClickRelatedVideo?.invoke(data)
            }
        }
    }

    private fun setTextCard(data: HomeBean.Issue.Item, holder: ViewHolder) {
        holder.setText(R.id.tv_text_card, data.data?.text!!)
        holder.getView<TextView>(R.id.tv_text_card).typeface = textTypeface
    }

    private fun setVideoDetailInfo(data: HomeBean.Issue.Item, holder: ViewHolder) {
        data.data?.title?.let {
            holder.setText(R.id.tv_title, it)
        }
        data.data?.description?.let {
            holder.setText(R.id.expandable_text, it)
        }
        holder.setText(R.id.tv_tag, "$${data.data?.category}/${durationFormat(data.data?.duration)}")
        holder.setText(R.id.tv_action_favorite, data.data?.consumption?.collectionCount.toString())
        holder.setText(R.id.tv_action_share, data.data?.consumption?.shareCount.toString())
        holder.setText(R.id.tv_action_reply, data.data?.consumption?.replyCount.toString())

        if (data.data?.author != null) {
            holder.apply {
                setText(R.id.tv_author_name, data.data.author.name)
                setText(R.id.tv_author_desc, data.data.author.description)
                setImagePath(R.id.iv_avatar, object : ViewHolder.HolderImageLoader(data.data.author.icon) {
                    override fun loadImage(iv: ImageView, path: String) {
                        val options = RequestOptions()
                                .centerCrop()
                                .placeholder(R.mipmap.placeholder_banner)
                                .transform(GlideRoundTransform(20))
                        Glide.with(mContext)
                                .load(path)
                                .apply(options)
                                .into(iv)
                    }
                })
            }
        } else {
            holder.setViewVisibility(R.id.layout_author_view, View.INVISIBLE)
        }
        holder.apply {
            getView<TextView>(R.id.tv_action_favorite).setOnClickListener {
                mContext.showToast("喜欢")
            }
            getView<TextView>(R.id.tv_action_share).setOnClickListener {
                mContext.showToast("分享")
            }
            getView<TextView>(R.id.tv_action_reply).setOnClickListener {
                mContext.showToast("评论")
            }
        }
    }

}